package com.hao.hospital.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;


import com.hao.hospital.model.PaymentRecord;
import com.hao.hospital.model.RegisterRecord;
import com.hao.hospital.model.User;
import com.hao.hospital.util.GeneralMethod;
import com.hao.hospital.util.TimeUtil;
import com.jfinal.aop.Enhancer;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.jlook.jfinal.ext3.kit.SqlBuilder;
import cn.jlook.jfinal.ext3.kit.SqlBuilder.JOIN;

public class PaymentService extends AbstractService {
	private static PaymentService me;

	public static PaymentService me() {
		if (null == me) {
			me = Enhancer.enhance(PaymentService.class);
		}
		return me;
	}
	
	
	public HashMap<String, Object> getEveryMonthUse(int use,String year){
		List<Record> list=Db.find("select YEAR(times) as year,MONTH ( times  ) as month,SUM(  money ) as money from payment_record where payment_record.use=? and  YEAR (  times  ) = ? GROUP BY MONTH (  times  )",use,2016);
		ArrayList<String> month=new ArrayList<>();
		ArrayList<BigDecimal> money=new ArrayList<>();
		for(Record r:list){
			 month.add( r.getInt("year")+"-"+r.getInt("month"));
			 money.add(r.getBigDecimal("money"));
		 }
		HashMap<String, Object> map=new HashMap<>();
		map.put("month",  month);
		map.put("money", money);
		return map;
	}
	
	/**
	 * 获取医生当天预约人的信息
	 * 
	 * @param person_id
	 * @return
	 */
	public List<Record> getTodayRegistertInfobyPersonId(int person_id) {
		long today = TimeUtil.me().getDayBeginTimestamp();
		System.out.println(today);
		long nextday = today + TimeUtil.me().ONE_DAY;
		System.out.println(nextday);
		List<Record> records = SqlBuilder.from(RegisterRecord.table).alias("r")
				.join(JOIN.LEFT, User.table, "u", "r.user_id=u.user_id")
				.where("r.person_id=? and r.create_time>=? and r.create_time<? and r.flag=1").field("*")
				.select(person_id, today, nextday);
		for (Record r : records) {
			r.set("CNsex", GeneralMethod.isSex(r.getInt("sex")));
		}
		return records;
	}

	/**
	 * 添加支付记录
	 * 
	 * @return
	 */
	public boolean addPayRecord(int userId, int money, long createtime, int use) {
		PaymentRecord paymentRecord = new PaymentRecord();
		paymentRecord.setMoney(money);
		paymentRecord.setTime(createtime);
		paymentRecord.setCreateTime(createtime);
		paymentRecord.setUserId(userId);
		paymentRecord.setUse(use);
		return paymentRecord.save();
	}

	/**
	 * 参数统计
	 * 
	 * @return
	 */
	public Record countArray(String... user_id) {
		System.out.println("1");
		Integer recharge, payment;
		Object o, b;
		if (StringUtils.isEmpty(user_id[0])) {
			System.out.println("2");
			o = SqlBuilder.from(PaymentRecord.table).field("SUM(money) as recharge").where("payment_record.use=2")
					.find().get("recharge");
			b = SqlBuilder.from(PaymentRecord.table).field("SUM(money) as payment").where("payment_record.use=1").find()
					.get("payment");
			if (o == null) {
				recharge = 0;
			} else {
				recharge = Integer.parseInt(o.toString());
			}
			if (b == null) {
				payment = 0;
			} else {
				payment = Integer.parseInt(b.toString());
			}

		} else {
			o = SqlBuilder.from(PaymentRecord.table).field("SUM(money) as recharge")
					.where("payment_record.use=2 and payment_record.user_id=?").find(user_id).get("recharge");
			b = SqlBuilder.from(PaymentRecord.table).field("SUM(money) as payment")
					.where("payment_record.use=1 and payment_record.user_id=?").find(user_id).get("payment");
			if (o == null) {
				recharge = 0;
			} else {
				recharge = Integer.parseInt(o.toString());
			}
			if (b == null) {
				payment = 0;
			} else {
				payment = Integer.parseInt(b.toString());
			}
		}
		System.out.println("运行到这了");
		Record record = new Record();
		record.set("in", recharge);
		record.set("out", payment);
		return record;
	}

	/**
	 * 全部用户交易记录
	 * 
	 * @param pageNumber
	 * @return
	 */
	public Page<Record> getPayRecord(int pageNumber, String... condition) {
		System.out.println("xxxx");
		String s = " ";
		System.out.println("xxxx");
		String c = GeneralMethod.isCondition(condition[0]);
		if ("支付".equals(condition[0])) {
			c = "1";
		} else if ("充值".equals(condition[0])) {
			c = "2";
		}
		if (!"'%'".equals(c)) {
			s = " or p.use=" + c;
		}
		System.out.println("获取支付记录");
		Page<Record> page = SqlBuilder.from(PaymentRecord.table).field("*").alias("p")
				.join(JOIN.LEFT, User.table, "u", "p.user_id=u.user_id").where(" (u.user_name like " + c + s + ")")
				.order("p.create_time desc").page(pageNumber, 8);
		List<Record> records = page.getList();
		for (Record r : records) {
			r.set("CNcreatetime", TimeUtil.me().stampToDateInYear(r.getLong("create_time").toString()));
			r.set("CNtime", TimeUtil.me().stampToDateInYear(r.getLong("time").toString()));
			r.set("CNsex", GeneralMethod.isSex(r.getInt("sex")));
			r.set("CNuse", GeneralMethod.isUse(r.getInt("use")));
		}
		return page;
	}

	/**
	 * 预约记录
	 * 
	 * @param userId
	 * @param personId
	 * @param worktime
	 * @param createtime
	 * @return
	 * @throws ParseException
	 */
	public boolean addRegisterRecord(int userId, int personId, Long worktime, long createtime) throws ParseException {
		RegisterRecord registerRecord = new RegisterRecord();
		System.out.println("1.1");
		registerRecord.setUserId(userId);
		System.out.println("1.2");
		registerRecord.setPersonId(personId);
		System.out.println("1.3");
		registerRecord.setWorktime(worktime);
		System.out.println("1.4");
		registerRecord.setCreateTime(createtime);
		System.out.println("1.5");

		try {
			return registerRecord.save();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
}
